const app = getApp();
var config = app.globalData.config;
const http = require('../../utils/http.js');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    this_week : [],
    real_week : [],
    selected : '',
    actual_total : 0,
    lever_total : 0,
    total : 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getWeekTime();
    this.getWeekTime_real();
    this.load_data();
  },

  onReady() {
		if (!app.login()) {
			return;
		}
  },
  
  load_data(){
    var that = this;
		http.httpPost(config.totalDinner, {
      datetime : that.data.selected
		}, function(e) {
      that.setData({
        actual_total : e.data.actual_total,
        lever_total : e.data.lever_total,
        total : e.data.total
      })
		});
  },

  // 获取当前一周的时间（日），两个方法混一起写，会错误
  getWeekTime() {
    var currentFirstDate;
    var formatDate = function(date) {
      var day = date.getDate() + "";
      return day;
    };

    var addDate = function(date, n) {
      date.setDate(date.getDate() + n);
      return date;
    };

    var setDate = function(date) {
      var week = date.getDay() - 1;
      date = addDate(date, week * -1);
      currentFirstDate = new Date(date);

      var arr = [];
      for (var i = 0; i < 7; i++) {
        var a = formatDate(i == 0 ? date : addDate(date, 1)); //星期一开始
        arr.push(a);
      }
      return arr;
    };
    setDate(new Date());
    var num = 0; //num为0时表示当前周，递增或递减时周数也递增或递减
    let setWeek = setDate(addDate(currentFirstDate, 7 * num));

    setWeek[new Date().getDay()] = '今';
    this.setData({
      this_week : setWeek
    })
  },

  // 获取当前一周的时间（年月日），两个方法混一起写，会错误
  getWeekTime_real() {
    var currentFirstDate;
    var formatDate = function(date) {
        var year = date.getFullYear() + "-";
        var month = date.getMonth() + 1 + "-";
        var day = date.getDate() + "";
        return year + month + day;
    };

    var addDate = function(date, n) {
      date.setDate(date.getDate() + n);
      return date;
    };

    var setDate = function(date) {
      var week = date.getDay() - 1;
      date = addDate(date, week * -1);
      currentFirstDate = new Date(date);

      var arr = [];
      for (var i = 0; i < 7; i++) {
        var a = formatDate(i == 0 ? date : addDate(date, 1)); //星期一开始
        arr.push(a);
      }
      return arr;
    };
    setDate(new Date());
    var num = 0; //num为0时表示当前周，递增或递减时周数也递增或递减
    let setWeek = setDate(addDate(currentFirstDate, 7 * num));
    this.setData({
      real_week : setWeek
    })
  },

  onDay(e){
    let key = e.currentTarget.dataset.key;
    console.log(key)
    this.setData({
      selected : key
    })
    this.load_data();
  }
})